package com.shycloud.mido.member.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.SmMemberGoldLog;
import com.shycloud.mido.member.api.entity.SmPrintPrice;
import com.shycloud.mido.member.mapper.MemberGoldLogMapper;
import com.shycloud.mido.member.service.MemberServiceV2;
import com.shycloud.mido.member.service.SmMemberGoldLogService;
import com.shycloud.mido.member.service.SmPrintPriceService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shycloud.mido.common.core.util.R;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 打印业务 controller
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/7/27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/print")
@ApiIgnore
public class PrintController {

	/** member 模块 service */
	private MemberServiceV2 memberService;
	/** member 拥有金币 service */
	private SmMemberGoldLogService memberGoldLogService;
	/** 打印模块 service */
	private SmPrintPriceService printPriceService;

	/**
	 * 获取用户打印次数
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/27 15:48
	 */
	@GetMapping("getPrint")
	@ApiOperation("获取用户打印次数")
	public R getPrint() {

		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();
		//根据用户ID获取用户信息
		Member member = memberService.getById(memberId);

		if (member == null) {
			return new R(CommonConstants.FAIL, "用户信息获取失败，请重新登录", false);
		}

		//初始化结果集 map
		Map<String, Integer> resultMap = MapUtil.newHashMap();
		//已用打印次数
		resultMap.put("printAmountUsed", member.getPrintAmountUsed());
		//赠送的剩余打印次数
		resultMap.put("printAmountGive", member.getPrintAmountGive());
		//购买的剩余打印次数
		resultMap.put("printAmountBuy", member.getPrintAmountBuy());
		//合计剩余打印次数
		resultMap.put("printAmountSurplus", member.getPrintAmountGive() + member.getPrintAmountBuy());
		//打印价格
		resultMap.put("printPrice", printPriceService.getOne(new QueryWrapper<>()).getPrice());
		return new R(resultMap);
	}

	/**
	 * 打印
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/27 16:04
	 */
	@GetMapping("print")
	@ApiOperation("打印")
	public R print(Integer musicId, String musicName) {
		return new R(printPriceService.print(musicId, musicName));
	}

}
